<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use addons\human\library\Speech;
use think\Db;
use addons\human\model\Job;

class Human extends Frontend
{

    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function index()
    {
        if($this->request->isPost()){
            $count=Db::name('human_job')->where('user_id',$this->auth->id)->count();
            // if($count>3){
            //   $this->error('免费用户只能生成三次视频');  
            // }
            $content=$this->request->post('content');
            $language=$this->request->post('language');
            $gender=$this->request->post('gender');
            $where=['language'=>$language,'gender'=>$gender];
            $voice=Db::name('human_voice')->where($where)->value('voice');
            if(!$voice){
                $this->error('该语种对应的性别语音不存在，请切换一个性别');
            }
            $character=$this->request->post('character');
            $style=$this->request->post('style');
            $bg=$this->request->post('bg');
            $speech=new Speech;
            $res=$speech->submitSynthesis($content,$character,$style,$voice,$bg);
            $this->success('提交成功','Human/job');
            
        }
        $persons=['lisa-casual-sitting','lisa-graceful-sitting','lisa-graceful-standing','jeff-business','jeff-formal','lori-casual','lori-graceful','lori-formal','meg-business','meg-formal','meg-casual','harry-business','harry-casual','harry-youthful'];
        $colors=['#FFFFFF','#18BC9C','#4E73DF','#605CA8','#F75444','#F39C12'];
        $this->view->assign('title', '虚拟人');
        $this->view->assign('persons',$persons);
        $this->view->assign('colors',$colors);
        return $this->view->fetch();
    }
    public function job()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $where = [];
            $filter = $this->request->request('filter');
            $filterArr = (array)json_decode($filter, true);
            
            if (isset($filterArr['createtime'])) {
                $timeArr = explode(' - ', $filterArr['createtime']);
                $where['createtime'] = ['between', [strtotime($timeArr[0]), strtotime($timeArr[1])]];
            }
            $search = $this->request->get('search');
            if ($search) {
                $where['filename'] = ['like', '%' . $search . '%'];
            }

            $model = new Job();
            $offset = $this->request->get("offset", 0);
            $limit = $this->request->get("limit", 0);
            $total = $model
                ->where($where)
                ->where('user_id', $this->auth->id)
                ->order("id", "DESC")
                ->count();

            $list = $model
                ->where($where)
                ->where('user_id', $this->auth->id)
                ->order("id", "DESC")
                ->limit($offset, $limit)
                ->select();

           
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $this->view->assign('title', '任务列表');
        return $this->view->fetch();
    }

}
